package io.gitee.zhangbinhub.acp.boot.listener

import io.gitee.zhangbinhub.acp.boot.init.AcpInitialization
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.ApplicationListener

/**
 * SpringBoot 应用初始化
 *
 * @since JDK 17
 */
class AcpApplicationStartupListener(
    private val logAdapter: LogAdapter,
    private val initializationList: MutableList<AcpInitialization>
) : ApplicationListener<ApplicationStartedEvent> {

    /**
     * 监听系统事件
     *
     * @param event 事件对象
     */
    override fun onApplicationEvent(event: ApplicationStartedEvent) {
        initializationList.sortedBy { it.order }.forEach { initialization ->
            logAdapter.info("start system initialization[" + initialization.order + "] : " + initialization.name)
            initialization.doInit()
        }
    }
}
